package com.longge;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-03-11 23:17
 */
public class RecoverTree {
    TreeNode one,two,pre;
    public void recoverTree(TreeNode root) {
        dfs(root);
        int temp = one.val;
        one.val=two.val;
        two.val=temp;
    }


    private void dfs(TreeNode root) {
        if (root==null) return;
        dfs(root.left);
        if (pre==null){
            pre=root;
        }
        if (root.val<pre.val){
            if (one==null){
                one=pre;
                two=root;
            }else {
                two=root;
            }
        };
        pre=root;
        dfs(root.right);
    }

    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);


        treeNode2.right=treeNode1;

        new RecoverTree().recoverTree(treeNode2);
    }
}
